<?php

namespace app\api\controller\v1;
use app\api\validate\Count;
use app\api\validate\IDMustBePostiveInt;
use app\api\model\Product as ProductModel;
use app\lib\exception\ProductExcption;

class Product
{
    public function getRecent($count = 15){
        (new Count())->goCheck($count);
        $products = ProductModel::getMostRecent($count);
        if($products->isEmpty ())
            throw new ProductExcption();
        $products = $products->hidden (['summary']);
        return $products;
    }

    public function getAllInCategory($id){
        (new IDMustBePostiveInt())->goCheck ($id);
        $products = ProductModel::getProductByCategoryID ($id);
        if($products->isEmpty ())
            throw new ProductExcption();
        $products = $products->hidden (['summary']);
        return $products;
    }

    public function getOne($id){
        (new IDMustBePostiveInt())->goCheck($id);
        $product = ProductModel::getProductDetail($id);
        if(!$product)
            throw new ProductExcption();
        return $product;
    }
}
